package com.huawei.fakedouyin.adapter;

import android.graphics.Color;
import android.graphics.Outline;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.BaseAdapter;

import com.huawei.fakedouyin.data.ColorBean;
import com.huawei.fakedouyin.databinding.GridItemBinding;
import com.huawei.fakedouyin.util.ActivityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 弹幕颜色选择的适配器
 */
public class GridAdapter extends BaseAdapter {

    public List<ColorBean> beanList = new ArrayList<>();

    public void updateData(List<ColorBean> beanList) {
        this.beanList = beanList;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return beanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        GridItemBinding binding = GridItemBinding.inflate(ActivityUtil.Companion.getInstance().getCurrentActivity().getLayoutInflater());
        binding.colorView.setBackgroundColor(Color.parseColor(beanList.get(i).getColor()));
        //选中的显示粉色打钩图标
        if (beanList.get(i).isSelect()){
            binding.colorSelect.setVisibility(View.VISIBLE);
        }else {
            binding.colorSelect.setVisibility(View.GONE);
        }
        setViewOutlineProvider(binding.getRoot());
        setViewOutlineProvider(binding.colorView);
        return binding.getRoot();
    }

    /**
     * view强制裁剪圆角
     * @param v
     */
    private void setViewOutlineProvider(View v){
        v.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),5);
            }
        });
        v.setClipToOutline(true);
    }
}
